package com.jml.springmvc.手写008.servlet.web;


import java.lang.reflect.Method;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;

/**
 * @author:jml
 * @date: 2019/10/11 15:18
 * @description：
 */
//使用了这个就不能有web.xml
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
	
	//先有servlet容器，再有servlet，onStartup在容器初始化的时候调用
	@Override
	public void onStartup(Set<Class<?>> classInfos, ServletContext ctx)
			throws ServletException {
		//classInfo这里就是AbstractDispatcherServletInitializer，因为这个是WebApplicationInitializer里面的子类
		for (Class<?> classInfo: classInfos) {
			Method method = null;
			try {
				//反射进行初始化
				method = classInfo.getMethod("onStartup", ServletContext.class);
				Object ob = classInfo.newInstance();
				method.invoke(ob,ctx);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
}
